﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace A2PongGame
{
    public class Bat : DrawableGameComponent
    {
        public Game1 game { get; set; }
        public Texture2D texture { get; set; }
        public SpriteBatch sp { get; set; }
        public Vector2 position;
        public Rectangle rectSource { get; set; }

        public void setY(int y) 
        { 
            rectSource = new Rectangle(rectSource.X, y + DeltaY, rectSource.Width, rectSource.Height);
            DeltaY = 0;
        }

        public int DeltaY;

        public Bat(Game game, Game1.Side side)
            : base(game)
        {
            sp = Game1.spriteBatch;
            position = new Vector2(100, 100);
            texture = new Texture2D(game.GraphicsDevice, 1, 1);
            texture.SetData(new Color[] { Color.White });
            this.side = side;
            if (side == Game1.Side.Left)
            {
                rectSource = new Rectangle(0, Game1.screenHeight / 2, 15, 80);
            }
            else
            {
                rectSource = new Rectangle(Game1.screenWidth - 15, Game1.screenHeight / 2, 15, 80);
            }


        }

        protected override void LoadContent()
        {

            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            sp.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
            sp.Draw(texture, rectSource, Color.White);
            sp.End();
            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            rectSource = new Rectangle(rectSource.X, rectSource.Y + DeltaY, rectSource.Width, rectSource.Height);
            DeltaY = 0;
            CheckCollision();

            base.Update(gameTime);
        }

        private void CheckCollision()
        {
            if (rectSource.Intersects(Game1.ball.rectSource))
            {
                Game1.ball.speedX *= -1;
            }
        }


        public Game1.Side side { get; set; }
    }
}
